package com.efounder.frame.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.core.xml.StubObject;
import com.efounder.frame.baseui.EFFragment;
import com.efounder.frame.tabbar.EFTabBar;
import com.efounder.frame.tabbar.EFTabBarItem;
import com.efounder.frame.utils.EFConstants;
import com.efounder.frame.xmlparse.EFXmlConstants;
import com.efounder.mobilemanager.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XinQing on 2016/8/29.
 */

public class EFAppAccountFragment extends EFFragment{
    private int position;
    private List<StubObject> twoLevelStubList;

    public static EFAppAccountFragment newInstance(int position, List<StubObject> twoLevelStubList){
        EFAppAccountFragment appAccountFragment = new EFAppAccountFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position",position);
        bundle.putSerializable("twoLevelStubList", (Serializable) twoLevelStubList);
        appAccountFragment.setArguments(bundle);
        return appAccountFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        position = getArguments().getInt("position");
        twoLevelStubList = (List<StubObject>) getArguments().getSerializable("twoLevelStubList");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ef_fragment_app_account,container,false);
        TextView textView = (TextView) view.findViewById(R.id.textView_test);
        textView.setText(position+"");
        LinearLayout tabBarLayout = (LinearLayout) view.findViewById(R.id.tabbarLayout);
        if (twoLevelStubList != null && twoLevelStubList.size() > 1){
            List<EFTabBarItem> tabBarItemList = new ArrayList<>();
            for (StubObject twoLevelStub:twoLevelStubList) {
                String imageName = twoLevelStub.getString(EFXmlConstants.ATTR_NORMAL_ICON,"");
                Drawable drawable = Drawable.createFromPath(EFConstants.APPS_DIR + "/App1/image/" + imageName);
                EFTabBarItem tabBarItem = new EFTabBarItem(getContext(),twoLevelStub.getCaption(),drawable);
                tabBarItemList.add(tabBarItem);
            }
            final EFTabBar tabBar = new EFTabBar(getContext(),tabBarItemList);
            tabBar.setTabSelectedColor(ResourcesCompat.getColor(getActivity().getResources(),R.color.chat_red,null));
            tabBar.setTabColor(ResourcesCompat.getColor(getActivity().getResources(),R.color.ef_gray_dark,null));
            tabBar.setTabPadding(20);
            tabBar.setTabTextSize(13);
            tabBar.setTabPadding(0);
            tabBar.setTabBackgroundColor(getActivity().getResources().getColor(R.color.ef_white));
            //FIXME 这句加上，点击才会切换颜色，EFTabBar selectedPosition 属性处置有问题
//            tabBar.setSelectedTab(0);
            tabBar.setOnTabBarItemClickListener(new EFTabBar.OnTabBarItemClickListener() {
                @Override
                public void onTabBarItemClick(EFTabBarItem EFTabBarItem, int position) {
//                    tabBar.setSelectedTab(position);
                }
            });


            tabBarLayout.addView(tabBar);
        }else {
            tabBarLayout.setVisibility(View.GONE);
        }

        return view;
    }
}
